#include <iostream>
#include "lexer.h"
#include "parser.h"
#include "codegen.h"
#include "stdio.h"
#include "Type.h"

void testCodeGen() {
    const char* code = "a=1;5+1-3*4/2";
    Lexer lex(code);
    lex.GetNextToken();

    Parser parser(lex);
    
    CodeGenerater code_gen;

    // visitor.VisitProgramNode();
    auto node = parser.Parse();
    node->Accept(&code_gen);
}

int main(int argc, char *argv[]) {
#if 1
    if (argc != 2) {
        printf("please input: C4_WITH_CLASS file_name\n");
        exit(1);
    }

    FILE *fp = fopen(argv[1], "r");
    if (fp == nullptr) {
        printf("file open failed!\n");
        exit(-1);
    }

    char buf[1024 * 10] = {0};
    size_t len = fread(buf, 1, sizeof(buf), fp);
    buf[len] = '\0';

    const char *code = buf;
    Lexer lex(code);
    lex.GetNextToken();

    Parser parser(lex);
    
    CodeGenerater code_gen;

    // visitor.VisitProgramNode();
    auto node = parser.Parse();
    node->Accept(TypeVisitor::Visitor());
    node->Accept(&code_gen);
#else
    const char *code = "a=1;5+1-3*4/2";

    Lexer lex(code);
    lex.GetNextToken();

    
#endif
    return 0;
}